<script>
import { updateAdmin } from '@/api/admin/admin.js'
import managerRules from '@/rules/managerRules.js'
import { selectManagerOptions } from '@/api/authApi.js'
export default {
  props: ['modelValue'],
  emits: ['update:modelValue', 'resetData'],
  computed: {
    flag: {
      get() {
        return this.modelValue
      },
      set(nv) {
        this.$emit('update:modelValue', nv)
      },
    },
  },
  data() {
    return {
      managerRules,
      managers: [],
      params: {
        role_id: '',
      },
      managerData: {
        admin_id: '',
        admin_username: '',
        admin_role_id: '',
      },
    }
  },

  methods: {
    async initManagerOptions() {
      this.managers = await selectManagerOptions(this.params)
    },
    remoteMethod(key) {
      this.params.managerData = key
      this.initManagerOptions()
    },
    async EditNewManager() {
      await this.$refs.form.validate()
      await updateAdmin(this.managerData)
      this.flag = false
      this.$emit('resetData')
    },
    resetFormData() {
      this.managerData = {}
      this.$refs.form.resetFields()
    },
    setDefaultForm(obj) {
      obj.admin_id = obj.admin_id
      obj.admin_username = obj.admin_username
      obj.admin_role_id = obj.role_id
      this.managerData = obj
    },
  },
  created() {
    this.initManagerOptions()
  },
}
</script>

<template>
  <el-dialog v-model="flag" title="管理员信息更新" width="600px" @closed="resetFormData">
    <el-form
      label-width="80px"
      :inline="false"
      ref="form"
      :model="managerData"
      :rules="managerRules"
    >
      <el-form-item label="管理员编号" prop="admin_username">
        <el-input type="text" v-model="managerData.admin_id"></el-input>
      </el-form-item>
      <el-form-item label="管理员账号" prop="admin_username">
        <el-input type="text" v-model="managerData.admin_username"></el-input>
      </el-form-item>
      <el-form-item label="管理员角色" prop="admin_role_id">
        <el-select
          clearable
          v-model="managerData.admin_role_id"
          filterable
          remote
          :remote-method="remoteMethod"
        >
          <el-option
            v-for="m in managers"
            :key="m.role_id"
            :value="m.role_id"
            :label="m.role_name"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="$emit('update:modelValue', nv)">取消</el-button>
      <el-button type="primary" @click="EditNewManager()">更新</el-button>
    </template>
  </el-dialog>
</template>
